#include <p24fj64GA006.h>
#include "def_bits.h"
#include "def_fonctions.h"


void SPIWrite(unsigned char data){
	SPI1BUF = data;
	while(SPI1STATbits.SPITBF);
}


unsigned char SPIRead(void){
	unsigned char data;
	if(SPI1STATbits.SPIRBF){
		data = SPI1BUF;
		SPI1STATbits.SPIROV = 0;
		return data;
	}
	SPI1BUF = 0xFF;  // write dummy data to initiate an SPI read
	while(SPI1STATbits.SPITBF); // wait until the data is finished reading
	data = SPI1BUF;
	SPI1STATbits.SPIROV = 0;
	return data;
}


void InitSPI(void){
	SDI_DIR = 1; 
	SCK_DIR = 0; // en mode maitre la clock est une sortie
	SDO_DIR = 0;

	SPI1STAT = 0x8000; // enable SPI port: 1000 0000 0000 0000
	SPI1CON1 = 0x0260;  // 0000 0001 0110 0000
}



